<?php
class IndexController extends Controller{

	public function index(){
		$d=isset($_GET['d'])?$_GET['d']:null;
		$d=addslashes(htmlentities($d));
		$d=basename($d);
		if($d && !is_null($d)){
			// 有存在d参数为遍历子目录
						$dirs=glob("date/".$d.'/*',GLOB_MARK);
			include './template/index.html';
		}else {
			// 没有存在d目录为遍历根目录
			$dirs=glob("date".'/*',GLOB_MARK);
			include './template/index.html';

		}

	}

	// 重命名数据
	public function restDate(){
		if($_GET['submit']!=1 && !IS_POST){
			return false;
		}
		$fd=$_POST['filialDir'];
		$fd=addslashes(htmlentities($fd));
		$fd=basename($fd);
		$newFileName=$_POST['newFileName'];
		$oldFileName=$_POST['oldFileName'];
		$data=array();
		// 如果有传入子集dir
		if($fd){
			foreach(glob("date/".$fd.'/*',GLOB_MARK) as $k=>$v){
				// var_dump($newNum);die;
				preg_match('/\d+/', $v, $num);
				$num=current($num);
				//重组后的路径
				$tempPath=str_replace('[num]', $num, $newFileName);
				$fullPath='date/'.$fd.'/'.$tempPath;
				copy($v,$fullPath);
				unlink($v);
				$data['oldFileName'][]=basename($v);
				$data['newFileName'][]=$tempPath;
			}

		}else{
			foreach(glob("date".'/*',GLOB_MARK) as $k=>$v){
			// var_dump($newNum);die;
			preg_match('/\d+/', $v, $num);
			$num=current($num);
			//重组后的路径
			$tempPath=str_replace('[num]', $num, $newFileName);
			$fullPath='date/'.$tempPath;
			copy($v,$fullPath);
			unlink($v);
			$data['oldFileName'][]=basename($v);
			$data['newFileName'][]=$tempPath;
		}
		}



		include './template/resetDate.html';

// Array
// (
//     ['old']=>
// 	    [0] => date/盗墓笔记第三季_001.mp3
// 	    [1] => date/盗墓笔记第三季_002.mp3
// 	    [2] => date/盗墓笔记第三季_003.mp3
// 	    [3] => date/盗墓笔记第三季_004.mp3
// 	    [4] => date/盗墓笔记第三季_005.mp3
// 	    [5] => date/盗墓笔记第三季_006.mp3
// 	['new']=>
// 		[0] => date/盗墓笔记第三季_001.mp3
// 	    [1] => date/盗墓笔记第三季_002.mp3
// 	    [2] => date/盗墓笔记第三季_003.mp3
// 	    [3] => date/盗墓笔记第三季_004.mp3
// 	    [4] => date/盗墓笔记第三季_005.mp3
// 	    [5] => date/盗墓笔记第三季_006.mp3
// )



	}

}

?>